package com.shuzijun.plantumlparser.cli;

import com.shuzijun.plantumlparser.cli.options.CliOptionsHolder;
import com.shuzijun.plantumlparser.core.ParserConfig;
import com.shuzijun.plantumlparser.core.ParserProgram;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;

public class CliRunner {

    private static String usage = "usage: plantuml-parser-cli [-f <arg>] [-fdef] [-fpri] [-fpro] [-fpub] [-h]\n" +
    "       [-l <arg>] [-mdef] [-mpri] [-mpro] [-mpub] [-o <arg>] [-scom]\n" +
    "       [-sctr] [-spkg]\n" +
    " -f,--filepath <arg>         Set the input file/directory\n" +
    " -fdef,--field_default       Add default fields\n" +
    " -fpri,--field_private       Add private fields\n" +
    " -fpro,--field_protected     Add protected fields\n" +
    " -fpub,--field_public        Add public fields\n" +
    " -h,--help                   Show the help\n" +
    " -l,--language_level <arg>   Set language level\n" +
    " -mdef,--method_default      Add default methods\n" +
    " -mpri,--method_private      Add private methods\n" +
    " -mpro,--method_protected    Add protected methods\n" +
    " -mpub,--method_public       Add public methods\n" +
    " -o,--outfile <arg>          Set the output file\n" +
    " -scom,--show_comment        Show comment\n" +
    " -sctr,--show_constructors   Show constructors\n" +
    " -spkg,--show_package        Show package";

    public static void main(String[] args) {

        if (args.length == 0) {
            System.out.println(usage);
            return;
        }

        try {
            final Options options = CliOptionsHolder.createOptions();
            final CommandLineParser parser = new DefaultParser();
            final CommandLine cmd = parser.parse(options, args);

            // Updated ParserConfig
            final ParserConfig config = new ParserConfig();
            CliOptionsHolder.getParserConfigUpdater()
                            .forEach(parserConfigUpdater -> parserConfigUpdater.updateConfig(config, cmd));

            final ParserProgram program = new ParserProgram(config);
            program.execute();
        } catch (final Exception ex) {
            ex.printStackTrace();
        }

    }

}
